Skip to content

XmlModel

Base classes

Name Children Inherits
BaseXmlModel
prettyqt.itemmodels.xmlmodel

⋔ Inheritance diagram

graph TD
  1498263208["itemmodels.XmlModel"]
  2676970995["itemmodels.BaseXmlModel"]
  3096575510["itemmodels.ColumnItemModel"]
  1030521844["itemmodels.ColumnItemModelMixin"]
  2751680898["builtins.object"]
  2846913429["itemmodels.TreeModel"]
  874904086["core.AbstractItemModel"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  2676970995 --> 1498263208
  3096575510 --> 2676970995
  1030521844 --> 3096575510
  2751680898 --> 1030521844
  2846913429 --> 3096575510
  874904086 --> 2846913429
  3882849576 --> 874904086
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  2751680898 --> 3882849576
  1898060022 --> 874904086
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: BaseXmlModel

Xml model for the builtin ElementTree. Child items are created lazily.

Model cant be modified, that only really makes sense for a full DOM implementation.

Source code in prettyqt/itemmodels/xmlmodel.py
class XmlModel(BaseXmlModel):
    """Xml model for the builtin ElementTree. Child items are created lazily.

    Model cant be modified, that only really makes sense for a full DOM implementation.
    """

    SUPPORTS = io.StringIO | str | datatypes.IsTreeIterator | ET.ElementTree | ET.Element

    def __init__(
        self,
        obj: str
        | io.StringIO
        | datatypes.IsTreeIterator
        | ET.ElementTree[Any]
        | ET.Element,
        show_root: bool = True,
        **kwargs,
    ):
        match obj:
            case io.StringIO():
                root = ET.parse(obj).getroot()
            case str():
                root = ET.fromstring(obj)
            case ET.Element():
                root = obj
            case ET.ElementTree():
                root = obj.getroot()
            case datatypes.IsTreeIterator():
                # consume the whole iterator, otherwise the tree is incomplete.
                for _ in obj:  # ty: ignore[not-iterable]  # an iterparse() iterator
                    pass
                root = obj.root
            case _:
                raise TypeError(obj)
        super().__init__(obj=root, show_root=show_root, **kwargs)

    @classmethod
    def supports(cls, instance) -> bool:
        match instance:
            case datatypes.IsTreeIterator() | ET.ElementTree() | ET.Element():
                return True
            case _:
                return False

    def get_parent_node(self, node_or_index: core.ModelIndex | ET.Element):
        # only lxml has Element.getparent()
        # for builtin Elements we need to go through indexes since we do not have a
        # full ElementTree.
        # might be worth having two XML models, a lazy one and a full-featured one.
        # Then we could use xpath to get parent here.
        if isinstance(node_or_index, core.ModelIndex):
            index = node_or_index
        elif indexes := self.search_tree(
            node_or_index, role=self.Roles.NodeRole, max_results=1
        ):
            index = indexes[0]
        else:
            return None
        parent = index.parent()
        return parent.data(self.Roles.NodeRole)

:fa-brands-github: Show source on GitHub